package com.richly.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 字段转化工具
 *
 * @author l_y
 * @email init_ly@foxmail.com
 * @date 2022-06-08 03:30:02
 */

public class DataConvertUtils {

    public static String convertNameJavaToColumn(String javaStr) {
        StringBuilder builder = new StringBuilder();
        if (null == javaStr || "".equals(javaStr)) {
            return builder.toString();
        }
        char[] chars = javaStr.toCharArray();
        for (char aChar : chars) {
            if (aChar < 97 && aChar > 64) {
                builder.append("_").append((char)(aChar + 32));
            } else {
                builder.append(aChar);
            }
        }
        return builder.toString();
    }

    public static <T> String convertDataType(T o) {
        if (o instanceof String) {
            return (String)o;
        }
        if (o instanceof BigDecimal) {
            return o.toString();
        }
        if (o instanceof Date) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format.format((Date)o);
        }
        if (o instanceof Integer) {
            return o.toString();
        }
        return null;
    };

}
